<template>
    <div class="register_container">
        <div class="register_box">
            <div class="register_remind">
                <i class="fas fa-users"> 注册</i>
            </div>
            <hr>
            <el-form class="register_form" :model="RgisterForm" status-icon :rules="RgisterRules" ref="RgisterFormRef">
                <el-form-item class="register_form_item" prop="username">
                    <el-input prefix-icon="fas fa-user" placeholder="用户名" v-model="RgisterForm.username"></el-input>
                </el-form-item>
                <el-form-item class="register_form_item" prop="password">
                    <el-input prefix-icon="fas fa-lock" type="password" placeholder="密码" v-model="RgisterForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item class="register_form_item" prop="checkPass">
                    <el-input prefix-icon="fas fa-lock" type="password" placeholder="确认密码" v-model="RgisterForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item class="register_form_item">
                    <el-button class="register_button" type="primary" @click="submitForm('RgisterFormRef')">免费注册</el-button>
                </el-form-item>
            </el-form>
            <div class="to_login">
                <p>已有账号？<a href="/login">登陆</a></p>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        // 密码检查
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.RgisterForm.checkPass !== '') {
                    this.$refs.RgisterFormRef.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.RgisterForm.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            RgisterForm: {
                username: '',
                checkPass: '',
                password: ''
            },
            RgisterRules: {
                username: [
                    { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    { validator: validatePass, trigger: 'blur' },
                    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' },
                    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(async valid => {
                if (!valid) return;
                const {data:res} = await this.$http.post('/user/register',this.RgisterForm);
               
                if(res.code == 200){
                    // this.$qs.stringify(this.RgisterForm)
                    this.$message({
                        message: res.message,
                        type: 'success'
                    });
                    this.$router.push({path:'/login'})
                //    console.log(res)
                }else{
                    this.$message.error(res.message);
                }
            });
        }
    }
}
</script>

<style lang="less" scoped>
.register_container{
    
}
.register_box {
    background-color: #f5f7fa;
    width: 500px;
    height: 550px;
    margin: auto;
    margin-top: 100px;
    border-radius: 20px;
    hr {
        width: 80%;
    }
}
.register_remind {

    text-align: center;
    padding-bottom: 10px;

    i {
        padding-top: 20px;
        font-size: 25px;
        font-weight: 800;
    }
}
.register_form {

    width: 80%;
    margin: auto;
    position: relative;
    top: 10px;

    
}
.register_form_item {

    padding-top: 20px;

}

.register_button {

    width: 100%;
}
.to_login {

    padding-top: 10px;
    text-align: center;
    a {
        text-decoration: none;
    }
}
</style>